<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            v-permission="permissionList.submitBtn"
            @click.stop="handleSubmit()"
            >提 交
          </el-button>
          <approve-button
            v-permission="permissionList.auditBtn"
            :approve-before="approveBefore"
            @approve="handleApprove"
            >审 批
          </approve-button>
          <el-button type="primary" size="small" @click.stop="handleExport()"
            >导 出
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click.stop="handleInputDetail()"
            >入 库
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click.stop="handleOutputDetail()"
            >出 库
          </el-button>
          <el-button
            type="primary"
            size="small"
            :hidden="true"
            v-if="false"
            @click.stop="handlePrint()"
            >打 印
          </el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="scope.row.checkStatus == '0' || scope.row.checkStatus == '3'"
            v-permission="permissionList.editBtn"
            @click.stop="rowEdit(scope.row, scope.index)"
            >编 辑
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            v-if="
              (scope.row.personId == userCode ||scope.row.personTwoId == userCode) &&
              (scope.row.checkStatus == '0' || scope.row.checkStatus == '3')
            "
            v-permission="permissionList.completeBtn"
            @click.stop="rowCompleteInfo(scope.row, scope.index)"
            >完善信息
          </el-button>
        </template>
      </avue-crud>
      <open-dialog
        :DialogVisible.sync="addVisableDialog"
        title="编辑"
        width="70%"
      >
        <el-form>
          <avue-form v-model="formAdd" :option="tableOptionEdit">
          </avue-form>
          <div class="bottom-menu">
            <el-button type="primary" @click="edit">保存</el-button>
            <el-button type="primary" @click="addVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
      <open-dialog
        :DialogVisible.sync="completeInfoVisableDialog"
        title="完善信息"
        width="70%"
      >
        <el-form>
          <avue-form
            v-model="formInfo"
            :option="tableOptionCompleteInfo"
          ></avue-form>
          <div class="bottom-menu">
            <el-button type="primary" @click="completeInfo">保存</el-button>
            <el-button type="primary" @click="completeInfoVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from "@/mixins/base-crud";
import { getObj, putObj,submit,inputDetailSubmit,outputDetailSubmit } from "@/api/subway/swstproduceplanday";
import {
  tableOptionEdit,
  tableOptionCompleteInfo,
} from "@/const/crud/subway/swstproduceplanday2";
import { dateFormat } from "@/util/date";
import store from "@/store";

export default {
  name: "StProducePlanDay",
  mixins: [baseCrud("subway/swstproduceplanday")],
  data() {
    return {
      exportName: "车票生产周计划.xlsx",
      formAdd: [],
      formInfo: [],
      addVisableDialog: false,
      completeInfoVisableDialog: false,
      tableOptionEdit: tableOptionEdit,
      tableOptionCompleteInfo: tableOptionCompleteInfo,
      userCode: store.getters.userInfo.userCode,
    };
  },
  computed: {
    permissionList() {
      return {
        submitBtn: this.vaildData(
          this.permissions.st_plan_day_produce_submit,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.st_plan_day_produce_edit,
          false
        ),
        completeBtn: this.vaildData(
          this.permissions.st_plan_day_produce_complete,
          false
        ),
        auditBtn: this.vaildData(
          this.permissions.st_plan_day_produce_audit,
          false
        ),
      };
    },
  },
  watch: {
    formInfo: {
      handler(newValue, oldValue) {
        if (newValue.effectiveNumber || newValue.invalidNumber) {
          newValue.totalNumber =
            newValue.effectiveNumber + newValue.invalidNumber;
        }
      },
      immediate: true,
      deep: true,
    },


    'formAdd.taskType'(val) {
        //获取渲染列
        // var cols = this.tableOptionEdit.group[0].column
        // var obj = null
        // debugger
        // cols.forEach((item) => {
        //   //找到操作对象 这里我们一取样规则为例 rule
        //   if (item.prop === 'esEquipmentType') {
        //     obj = item
        //   }
        // })
        //获取渲染列
        var issuingMethod = this.findObject(this.tableOptionEdit.group[0].column, "issuingMethod");
        var esEquipmentType = this.findObject(this.tableOptionEdit.group[0].column, "esEquipmentType");
        var esTicketType = this.findObject(this.tableOptionEdit.group[0].column, "esTicketType");
        var esIsInitialization = this.findObject(this.tableOptionEdit.group[0].column, "esIsInitialization");
        var esAppTypes = this.findObject(this.tableOptionEdit.group[0].column, "esAppTypes");
        var esStartStation = this.findObject(this.tableOptionEdit.group[0].column, "esStartStation");
        var esEndStation = this.findObject(this.tableOptionEdit.group[0].column, "esEndStation");
        var esStartDate = this.findObject(this.tableOptionEdit.group[0].column, "esStartDate");
        var esEndDate = this.findObject(this.tableOptionEdit.group[0].column, "esEndDate");
        //根据需要进行操作，比如隐藏display:true 禁用 disabled
        //任务类型:1:票卡初始化任务、2:票卡预赋值任务、3:票卡重编码任务、4:票卡注销任务
        if (val == '1') {
          esEquipmentType.display = true
          esTicketType.display = true
          esIsInitialization.display = true

          issuingMethod.display = false
          this.formAdd.issuingMethod = ''
          esAppTypes.display = false
          this.formAdd.esAppTypes = ''
          esStartStation.display = false
          this.formAdd.esStartStation = ''
          esEndStation.display = false
          this.formAdd.esEndStation = ''
          esStartDate.display = false
          esEndDate.display = false

        }
        if (val == '2') {
          esEquipmentType.display = true
          issuingMethod.display = true
          esAppTypes.display = true
          esIsInitialization.display = true
          esStartStation.display = true
          esEndStation.display = true
          esStartDate.display = true
          esEndDate.display = true

          esTicketType.display = false
          this.formAdd.esTicketType = ''

        }
        if (val == '3') {
          esEquipmentType.display = true
          issuingMethod.display = true
          esTicketType.display = true
          esStartStation.display = true
          esEndStation.display = true
          esStartDate.display = true
          esEndDate.display = true

          esAppTypes.display = false
          this.formAdd.esAppTypes = ''
          esIsInitialization.display = false
          this.formAdd.esIsInitialization = ''

        }
        if (val == '4') {
          esEquipmentType.display = true
          esTicketType.display = true

          issuingMethod.display = false
          this.formAdd.issuingMethod = ''
          esAppTypes.display = false
          this.formAdd.esAppTypes = ''
          esIsInitialization.display = false
          this.formAdd.esIsInitialization = ''
          esStartStation.display = false
          this.formAdd.esStartStation = ''
          esEndStation.display = false
          this.formAdd.esEndStation = ''
          esStartDate.display = false
          esEndDate.display = false

        }
    }
  },
  methods: {
    handleSubmit: function() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.checkStatus != "0"&&table.checkStatus != "3") {
          this.$message.error("请检查数据，只能提交未提交的数据！");
          return false;
        }
        if (table.actualFinishTime == null || (table.totalNumber == null||table.totalNumber == 0)) {
          this.$message.error("请检查，选择的数据有未完善信息的，请完善信息！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        submit(ids)
          .then(data => {
            this.$message.success("提交成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    handleInputDetail:function(){
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (this.validatenull(table.personName)) {
          this.$message.error("请检查选择的数据有未填写车票编码员的数据，请先填写车票编码员！");
          return false;
        }
        if (this.validatenull(table.actualFinishTime) || (this.validatenull(table.totalNumber)||table.totalNumber == 0)) {
          this.$message.error("请检查选择的数据有未完善信息的，请先完善信息！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认加入入库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        inputDetailSubmit(ids)
          .then(data => {
            this.$message.success("入库成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    handleOutputDetail:function(){
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (this.validatenull(table.personName)) {
          this.$message.error("请检查选择的数据有未填写车票编码员的数据，请先填写车票编码员！");
          return false;
        }
        if (this.validatenull(table.actualFinishTime) || (this.validatenull(table.totalNumber)||table.totalNumber == 0)) {
          this.$message.error("请检查选择的数据有未完善信息的，请先完善信息！");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认加入出库?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        outputDetailSubmit(ids)
          .then(data => {
            this.$message.success("出库成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
    rowEdit: function (row) {
      row.$cellEdit = true;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formAdd = res;
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate(now.getDate() - 1);
          }
          this.formAdd.actualFinishTime = dateFormat(now);
        })
        .then(() => {
          this.addVisableDialog = true;
        });
    },
    edit: function () {
      if (this.formAdd.taskType == "" || this.formAdd.taskType == null) {
          this.$message.error("请选择任务类型！");
          return;
      }
      if (this.formAdd.startEndNo == "" || this.formAdd.startEndNo == null) {
          this.$message.error("请按照格式输入起号-止号！");
          return;
      }
      putObj(this.formAdd)
        .then((data) => {
          this.$message.success("修改成功");
          this.addVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    rowCompleteInfo: function (row) {
      row.$cellEdit = true;
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formInfo = res;
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate(now.getDate() - 1);
          }
          this.formInfo.actualFinishTime = dateFormat(now);
        })
        .then(() => {
          this.completeInfoVisableDialog = true;
        });
    },
    completeInfo: function () {
      putObj(this.formInfo)
        .then((data) => {
          this.$message.success("完善信息成功");
          this.completeInfoVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    approveBefore(callback) {
      var tableSelect = this.$refs.crud.tableSelect;
      if (tableSelect.length == 0) {
        this.$message.info("请选择要审批的记录");
        return;
      }
      for (let row of tableSelect) {
        if (row.checkStatus != "1") {
          this.$message.info(
            `序号为${row.$index + 1}的记录不是已提交状态不能审批`
          );
          return;
        }
      }
      callback();
    },
  },
};
</script>
<style lang="scss" scoped>
.bottom-menu {
  text-align: center;
}
</style>
